<?php
class ProductModel extends RelationModel {

    protected $_link = array(
        "brand" => array(
            "mapping_type" => BELONGS_TO,
            "class_name" => "product_brand",
            "mapping_name" => "brand",
            "foreign_key" => "brand_id",
            "mapping_fields" => array("id", "name", "alias"),
        ),
        "description" => array(
            "mapping_type" => HAS_MANY,
            "class_name" => "product_description",
            "mapping_name" => "description",     
            "foreign_key" => "product_id",
        ),
        "multi_img" => array(
            "mapping_type" => HAS_MANY,
            "class_name" => "product_image",    
            "mapping_name" => "multi_img",
            "foreign_key" => "product_id",
        ),
        "category_tag" => array(
            "mapping_type" => MANY_TO_MANY,    
            "class_name" => "product_category_tag",
            "mapping_name" => "category_tag",
            "foreign_key" => "product_id",
            "relation_foreign_key" => "category_tag_id",
            "relation_table" => "product_to_category_tag",
        ),
        "product_suppliers" => array(
            "mapping_type" => MANY_TO_MANY,    
            "class_name" => "product_suppliers",
            "mapping_name" => "suppliers",
            "foreign_key" => "product_id",
            "relation_foreign_key" => "suppliers_id",
            "relation_table" => "product_to_suppliers",
        ),
    );
    
    //通过分类ID获取产品ID
    function getIdsByCategoryId($cids){
    	$category_m = M('product_to_category_tag');
    	$result     = $category_m->where("category_tag_id IN ($cids)")->select();
    	$prodcut_ids = '0';
    	if ( $result )
    	foreach ( $result as $value ) {
    		$prodcut_ids .= ','.$value['product_id'];
    	}
    	return $prodcut_ids;
    }

}

